package com.sskj.asset.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.asset.R;
import com.sskj.asset.R2;
import com.sskj.asset.bean.AssetBean;
import com.sskj.asset.bean.AssetListBean;
import com.sskj.asset.component.DaggerUserDataComponent;
import com.sskj.asset.presenter.AssetManageActivityPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.CoinUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.ASSET_MANAGE)//资产管理
public class AssetManageActivity extends BaseActivity<AssetManageActivityPresenter> {
    @BindView(R2.id.tv_asset)
    TextView tvAsset;
    @BindView(R2.id.tv_asset_cny)
    TextView tvAssetCny;
    @BindView(R2.id.btn_recharge)
    Button btnRecharge;
    @BindView(R2.id.btn_withdraw)
    Button btnWithdraw;
    @BindView(R2.id.btn_exchange)
    Button btnExchange;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    DecimalFormat mFormat = new DecimalFormat("0.0000");
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.asset_activity_asset_manage;
    }

    @Override
    public AssetManageActivityPresenter getPresenter() {
        return new AssetManageActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.asset_assetManageActivity1));
        DaggerUserDataComponent.create().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(this, 2))
                .setLeftPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(this, 30))
                .setDividerColor(ContextCompat.getColor(this, R.color.asset_1c1)));
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.asset_recy_item_asset, new SlimInjector<AssetListBean>() {
            @Override
            public void onInject(AssetListBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.item_asset_manage_type, data.getPname());
                injector.text(R.id.item_asset_manage_available_balance, CoinUtil.keepCoinNumMax(data.getPname(), data.getUsable()));
                injector.text(R.id.item_asset_manage_frozen_capital, data.getFrost());
                injector.text(R.id.item_asset_manage_cny, data.getTwdmoney());
                injector.clicked(R.id.item_asset_manage_record, v -> {//账户明细

                        ARouter.getInstance().build(RConfig.MINE_ASSET_RECORDS).withString("pid", data.getPid()).navigation();

                });
                injector.text(R.id.available_text, App.INSTANCE.getString(R.string.asset_assetManageActivity2) + "(" + data.getPname() + ")");
                injector.text(R.id.freeze_text, App.INSTANCE.getString(R.string.asset_assetManageActivity3) + "(" + data.getPname() + ")");
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<AssetListBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<AssetListBean>)
                page -> mPresenter.getData(page + "", "20"), 20);
        MVCCoolHelper<List<AssetListBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    protected void initData() {
        mPresenter.getTop();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });
    }

    public void updateUI(AssetBean.ResBean.TotalBean total) {
        tvAsset.setText(String.format("≈%s USDT",CoinUtil.keepUSDT(total.getTtl_money())));
        tvAssetCny.setText( total.getTtl_twdmoney());
        ClickUtil.click(btnRecharge, () -> {
            ARouter.getInstance().build(RConfig.MINE_RECHARGE).navigation();
        });

        ClickUtil.click(btnWithdraw, () -> {
            if (!AppCircleCheckUtil.checkAuth(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.MINE_WITHDRAW).navigation();
        });
        ClickUtil.click(btnExchange, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(this, userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.ASSET_CASH).navigation();

        });
    }
}
